package com.tuishan.myapplication.Views.Fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.tuishan.myapplication.Adapters.HomeAdapter;
import com.tuishan.myapplication.Custom.LoadingView;
import com.tuishan.myapplication.Model.PageNumModel;
import com.tuishan.myapplication.Model.ProductModel;
import com.tuishan.myapplication.Model.UserLogModel;
import com.tuishan.myapplication.R;
import com.tuishan.myapplication.Servers.BaseCallback;
import com.tuishan.myapplication.Servers.OkHttpHelper;
import com.tuishan.myapplication.Servers.UrlRouter;

import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;

import cn.qqtheme.framework.picker.OptionPicker;
import okhttp3.Request;
import okhttp3.Response;

/**
 * <pre>
 *     time   : 2021/04/18
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class Fragment_Home extends Fragment implements View.OnClickListener, OnTitleBarListener {
    LoadingView Loading;
    RecyclerView Home_recyclerView;
    List<ProductModel> productModelList = new ArrayList<>();
    HomeAdapter adapter;
    TitleBar titleBar;
    String[] TypeList = new String[]{
            "全部", "生活", "学习", "服饰", "食品", "家具", "其他"
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_layout, container, false);
        Home_recyclerView = view.findViewById(R.id.Home_recyclerView);
        titleBar = view.findViewById(R.id.titleBar);
        Loading = new LoadingView(getActivity());
        titleBar.setOnTitleBarListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        Home_recyclerView.setLayoutManager(layoutManager);
        adapter = new HomeAdapter(productModelList, Loading, getActivity());
        Home_recyclerView.setAdapter(adapter);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        GetTransList(1, 200, 0, 0);
    }

    @Override
    public void onClick(View view) {

    }

    private void GetTransList(int pageNum, int pageSize, int productType, int usage) {
        Loading.show();
        WeakHashMap<String, Object> objectMap = new WeakHashMap<>();
        objectMap.put("pageNum", pageNum);
        objectMap.put("pageSize", pageSize);
        objectMap.put("productType", productType);
        objectMap.put("usage", usage);
        OkHttpHelper mHttpHelper = OkHttpHelper.getInstance();
        mHttpHelper.post(UrlRouter.TransactListUrl, String.class, objectMap, new BaseCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                Loading.dismiss();
            }

            @Override
            public void onSuccess(Response response, Object arg1) {
                Loading.dismiss();
                Gson gson = new Gson();
                PageNumModel<ProductModel> pageNumModel = gson.fromJson(arg1.toString(),
                        new TypeToken<PageNumModel<ProductModel>>() {
                        }.getType());
                productModelList.clear();
                productModelList.addAll(pageNumModel.getList());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFalse(Response response, String err_msg) {
                Loading.dismiss();
                Toast.makeText(getActivity(), err_msg, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
                Loading.dismiss();
            }
        });
    }

    @Override
    public void onLeftClick(View v) {

    }

    @Override
    public void onTitleClick(View v) {

    }

    @Override
    public void onRightClick(View v) {
        ShowTypePicker();
    }

    //类型选择器
    private void ShowTypePicker() {
        OptionPicker picker = new OptionPicker(getActivity(), TypeList);
        picker.setOffset(2);
        picker.setSelectedIndex(1);
        picker.setTitleText("选择类型");
        picker.setTextSize(24);
        picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
            @Override
            public void onOptionPicked(int index, String item) {
                GetTransList(1, 200, index, 0);
            }
        });
        picker.show();
    }
}
